package com.kgcx.system.service;

import com.kgcx.system.domain.SysRole;
import com.kgcx.system.domain.SysRoleMenu;
import com.kgcx.system.mapper.SysEmployeeRoleMapper;
import com.kgcx.system.mapper.SysRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 何凯
 * @version 0.1
 * @description 角色业务层
 * @time 2017/12/13 17:12
 * @since 0.1
 */
@Service
public class SysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Resource
	private SysEmployeeRoleMapper sysEmployeeRoleMapper;

    /**
     * 新增角色
     * @param role
     */
    public void saveSysRole(SysRole role){
        sysRoleMapper.insert(role);

    }
    /**
     * 删除角色
     * @param kid
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteSysRole(Integer kid){
        sysRoleMapper.deleteByPrimaryKey(kid);
        sysRoleMenuService.deleteByRoleId(kid);
        sysEmployeeRoleMapper.deleteByRoleId(Long.valueOf(kid));


    }
    /**
     * 修改角色 和菜单
     * @param role
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateSysRoleAndMenu(SysRole role){
        sysRoleMenuService.deleteByRoleId(role.getKid());
        sysRoleMenuService.insertByBatch(role.getMenuList());
        sysRoleMapper.updateByPrimaryKeySelective(role);
    }
    /**
     * 修改角色
     * @param role
     */
    public void updateSysRole(SysRole role){
        sysRoleMapper.updateByPrimaryKeySelective(role);
    }
    /**
     * 查询角色
     * @param kid
     */

    public SysRole getSysRole(Integer kid){
       return sysRoleMapper.selectRoleMenuByRoleId(kid);
    }

    /**
     * 查询多条角色
     * @param role
     * @return
     */
    public List<SysRole> querySysRoleList(SysRole role){
        return sysRoleMapper.selectRoleList(role);
    }
    /**
     * 查询多条角色
     * @param role
     * @return
     */
    public List<SysRole> querySysRoleListByName(SysRole role){
        return sysRoleMapper.selectRoleListByName(role);
    }
    /**
     * 保存角色
     * @param sysRole
     */
    public void saveSysRoleMenu(SysRole sysRole){
        sysRoleMapper.insertSelective(sysRole);
        /*if(null!=sysRole.getMenuList()&&sysRole.getMenuList().size()>0){
            for(SysRoleMenu menu :sysRole.getMenuList()){
            menu.setRoleId(sysRole.getKid());
            }
          sysRoleMenuService.insertByBatch(sysRole.getMenuList());
        }*/
    }

}
